package com.fengba.nanzhi.tabbar01;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.TextView;

import com.fengba.nanzhi.fengba.MainActivity;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.tabbar03.SubmitOrder;
import com.fengba.nanzhi.tabbar03.SubmitOrder_Psq;
import com.fengba.nanzhi.utils.OKManager;

public class FX_psq_game extends AppCompatActivity {

    private WebView webview;
    private String invite_code;
    private ImageButton mBtntjmfh;
    private String id;
    private String type;
    private String crazyrob_id;
    private TextView shdztitle;
    Handler handler = new Handler();
    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_psq_game);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        type = intent.getStringExtra("type");
        crazyrob_id = intent.getStringExtra("crazyrob_id");
        mBtntjmfh = findViewById(R.id.FPGtb01);
        mBtntjmfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //http://www.nan-zhi.com/app/api/crazygame
        webview = findViewById(R.id.FPGwv01);
        //类型（1大转盘，2翻牌子，3砸金蛋，4跑马灯，5刮刮乐）',
        shdztitle = findViewById(R.id.shdztitle);
        switch(type)
        {
            case "1":
                shdztitle.setText("大转盘");
                break;
            case "2":
                shdztitle.setText("翻牌子");
                break;
            case "3":
                shdztitle.setText("砸金蛋");
                break;
            case "4":
                shdztitle.setText("跑马灯");
                break;
            case "5":
                shdztitle.setText("刮刮乐");
                break;
            default:
                shdztitle.setText("游戏");
        }
        configWebView(webview, false);
        //String url = "http://www.nan-zhi.com/app/api/crazygame?user_id="+id+"&type="+type+"&crazyrob_id="+crazyrob_id;
        //String url = "http://www.nan-zhi.com/app/api/crazygame";
        String url = "http://www.nan-zhi.com/app/api/crazygame?user_id="+id+"&type="+type+"&crazyrob_id="+crazyrob_id;
        String data = "user_id="+id+"&type="+type+"&crazyrob_id="+crazyrob_id;
        //Log.e("session-p","user_id="+id+"&type="+type+"&crazyrob_id="+crazyrob_id);
        //Log.e("session","nzpay.sesssion="+ OKManager.getInstance().sessionlogin);
        synCookies(this, url, "nzpay.sesssion="+ OKManager.getInstance().sessionlogin);
        synCookies(this, url, "rememberMe=deleteMe");
        webview.loadUrl(url);
        //webview.postUrl(url,data.getBytes());
//        ////System.out.println("http://192.168.3.103:8888/api/crazygame?user_id="+id+"&type="+type+"&crazyrob_id="+crazyrob_id);
//        webview.getSettings().setJavaScriptEnabled(true);
//        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        webview.getSettings().setLoadWithOverviewMode(true);
//
//        //设置是否出现缩放工具
//        webview.getSettings().setBuiltInZoomControls(true);
//        //设置可以支持缩放
//        webview.getSettings().setSupportZoom(true);
//        //扩大比例的缩放
//        webview.getSettings().setUseWideViewPort(true);
//
//        //设置编码为utf-8
//        webview.getSettings().setDefaultTextEncodingName("utf-8");
//
//        webview.getSettings().setJavaScriptEnabled(true);
//        webview.getSettings().setAppCacheEnabled(true);
//        //设置 缓存模式
//        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//        // 开启 DOM storage API 功能
//        webview.getSettings().setDomStorageEnabled(true);

//        webview.setWebViewClient(new WebViewClient() {
//            //覆盖shouldOverrideUrlLoading 方法
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//        });

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return false;
    }

    /**
     * 设置Cookie
     *
     * @param context
     * @param url
     * @param cookie  格式：uid=21233 如需设置多个，需要多次调用
     */
    public void synCookies(Context context, String url, String cookie) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.setCookie(url, cookie);//cookies是在HttpClient中获得的cookie
        CookieSyncManager.getInstance().sync();
    }

    /**
     * 清除Cookie
     *
     * @param context
     */
    public static void removeCookie(Context context) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        CookieSyncManager.getInstance().sync();
    }


    protected void configWebView(WebView webView, boolean needCache) {

        if (!needCache) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        }

        webView.getSettings().setDefaultTextEncodingName("utf-8");//设置网页默认编码
        webView.getSettings().setJavaScriptEnabled(true);
        //启用数据库
        webView.getSettings().setDatabaseEnabled(true);
        //设置定位的数据库路径
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        webView.getSettings().setGeolocationDatabasePath(dir);
        //启用地理定位
        webView.getSettings().setGeolocationEnabled(true);
        //开启DomStorage缓存
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.setWebViewClient(new WebViewClient(){});
        webView.setWebChromeClient(new WebChromeClient());
        // 通过addJavascriptInterface()将Java对象映射到JS对象
        //参数1：Javascript对象名
        //参数2：Java对象名
        JavaScriptInterface myJavaScriptInterface
                = new JavaScriptInterface(this);
        webView.addJavascriptInterface(myJavaScriptInterface, "android");//AndroidtoJS类对象映射到js的test对象
    }

    // 继承自Object类
    public class JavaScriptInterface {

        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;
        }

        // 定义JS需要调用的方法
        // 被JS调用的方法必须加入@JavascriptInterface注解
        @JavascriptInterface
        public void shop(String msg) {
            //System.out.println(msg);
            String good = msg;
            Intent intent = new Intent(mContext, SubmitOrder_Psq.class);
            intent.putExtra("good", good.toString());
            startActivity(intent);
        }
    }
}

